<?
/*Created by : G.M.Sundar 
[created : 01/17/2007, 06:31:28 PM]
 Description:

*/
include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/fpdf.php");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
include(DOCINCLUDES."global_files/global_pdf.inc");

$db=new cDatabase($company_id);
$sql=new pgSql();//
global $pdf;
$pdf=new FPDF('p','mm','A4');
$fontname="Arial";
$fontsize="8";
$cellheight = 3;
if($_POST)
	{
	$commercial_id = $_POST["commercial_id"];
	}
elseif($_GET)
	{

	$commercial_id = $_GET["commercial_id"];
	}

//removed column discount
$db->query="select buyer as\"Buyer Name\",buyer_address as\"Buyer Address\",buyer_city as\"Buyer City\",buyer_state as\"Buyer State\",buyer_country as\"Buyer Country\",buyer_zipcode as\"Buyer ZipCode\",company_id as\"Company Name\",company_address as\"Address\",company_city as\"City\",company_state as\"State\",company_country as\"Country\",company_zipcode as\"ZipCode\",pre_carriage  as\"Pre Carriage By\",place_of_receipt as\"Place of Reciept\",from_city as\"From City\",discharge as\"Port of Discharge\",destination as\"Port of Destination\",marks_and_nos as\"Mark & Nos\",no_of_pkgs as\"No & Kind of Pkgs\",description as\"Description\",quantity_type_id as\"Quantity\",value as\"Value\",name_of_signatory as\"Authorised Signatory\",shipment_mode_name,total_val,total_qty from $certificate_of_origin_pdf_view where commercial_id=$commercial_id;";
$db->runQuery();
$commercial_array=$db->returnArrays();

 $db->query="SELECT
item_desc as \"item Description\",
remarks as \"Remarks\",
qty as \"Quantity\",
val as \"Value\"
from $coo_item_details_table where commercial_id=$commercial_id order by item_no;";

$db->runQuery();
$items_array=$db->returnArrays();



//Creating PDF

if($commercial_array[0]['Buyer Name']!='NO BUYER')
{
if($commercial_array[0]['Buyer Name'])
$buyer_details=$commercial_array[0]['Buyer Name']."\n";
if($commercial_array[0]['Buyer Address'])
$buyer_details.=$commercial_array[0]['Buyer Address']."\n";
if($commercial_array[0]['Buyer City'])
$buyer_details.=$commercial_array[0]['Buyer City']." - ";
if($commercial_array[0]['Buyer ZipCode'])
$buyer_details.=$commercial_array[0]['Buyer ZipCode']."\n";
if($commercial_array[0]['Buyer State'])
$buyer_details.=$commercial_array[0]['Buyer State']." ";
if($commercial_array[0]['Buyer Country'])
$buyer_details.=$commercial_array[0]['Buyer Country'].".";

}

if($commercial_array[0]['Exporter Name']!='NO SUPPLIER')
{
if($commercial_array[0]['Company Name'])
$sender_details=$commercial_array[0]['Company Name']."\n";
if($commercial_array[0]['Address'])
$sender_details.=$commercial_array[0]['Address']."\n";
if($commercial_array[0]['City'])
$sender_details.=$commercial_array[0]['City']." - ";
if($commercial_array[0]['ZipCode'])
$sender_details.=$commercial_array[0]['ZipCode']."\n";
if($commercial_array[0]['State'])
$sender_details.=$commercial_array[0]['State']." ";
if($commercial_array[0]['Country'])
$sender_details.=$commercial_array[0]['Country'].".";
}

// $item_count=0;
// $maxline=array();
// 
// foreach($items_array as $key=>$value)
// {
// foreach($value as $key1=>$value1)
// {
// if($maxline[$item_count-1]<=28)
// {
// $data=(countlines($value1,31,"\n")>$data)?countlines($value1,31,"\n"):$data;
// $maxline[$item_count]=$data+$maxline[$item_count-1];
// }
// else
// {
// $data=(countlines($value1,31,"\n")>$data)?countlines($value1,31,"\n"):$data;
// $maxline[$item_count]=$data;
// 
// }
// 
// }
// $item_count++;
// }


$pagecount=0;
$i=0;
//array used to specify width of the column
$columnsize=array(25,10,20,20);

//array used to specify starting point of each column
$width_array=array(140,110,124,140);

$pdfstring=makeString($items_array,$columnsize);

$start=0;
$no_of_lines=16;

foreach($pdfstring as $key=>$value)
{

$pdf->ln(6) ;

foreach ($value as $key1=>$value1)
{

 if($i==$no_of_lines||$start==0)
{
$i=0;
dopdfheader();

if($strlinecount)
	$strlinecount=($strlinecount*2);
/*
if($book_nos_array[$pagecount-2])
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+123,$LEFTMARGIN+55,"(i.e., continued from EI No.".$book_nos_array[$pagecount-2].")",$wrapsize
 = 50,$wrapbreak = "\n" , $cellwidth =100,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
*/
}


$desc_lines=countlines($commercial_array[0]['Description'],30,'.');
//echo $desc_lines;
$item_top_margin=($i*$cellheight)+125+($desc_lines*3);

 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+81,$value1[0],$wrapsize =
 25,$wrapbreak = "\n" , $cellwidth =45,$cellheight
 =3,$cellborder=0,$cellalign="L",$cellfill=0);
 
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+125,$value1[1],$wrapsize
 =9,$wrapbreak = "\n" , $cellwidth = 14,
 $cellheight = 3 ,$cellborder=0, $cellalign="L",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+135,$value1[2],$wrapsize
 =
 20,$wrapbreak = "\n" , $cellwidth =30,
 $cellheight = 3 ,$cellborder=0, $cellalign="R",$cellfill=0);
 
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+170,$value1[3],$wrapsize =
 20,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight = 3 ,$cellborder=0, $cellalign="R",$cellfill=0);
 /*
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+165,$value1[4],$wrapsize =
 8,$wrapbreak = "\n" , $cellwidth = 20,
 $cellheight  ,$cellborder=190, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+$item_top_margin,$LEFTMARGIN+185,$value1[5],$wrapsize = 15,
 $wrapbreak = "\n" , $cellwidth = 25,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);*/
$i++;

if($i==$no_of_lines||$start==0)
{

// if($book_nos_array[$pagecount])
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+225,$LEFTMARGIN+55,"(i.e., Covering the continued in EI No.".$book_nos_array[$pagecount].")",$wrapsize = 50,
//  $wrapbreak = "\n" , $cellwidth = 100,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
dopdffooter();

$start=1;


}

}

}

// $pdf->AddPage();
// 
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+50,$LEFTMARGIN+20,$commercial_array[0]['remarks'],$wrapsize = 150,
//  $wrapbreak = "\n" , $cellwidth = 150,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+76,$LEFTMARGIN+20,$commercial_array[0]['origin_requirements'],$wrapsize = 150,
//  $wrapbreak = "\n" , $cellwidth = 150,
//  $cellheight = 6 ,$cellborder=0, $cellalign="L",$cellfill=0);
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+99,$LEFTMARGIN+20,$items_array[0]['nos_invoice_date'],$wrapsize = 150,
//  $wrapbreak = "\n" , $cellwidth = 150,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
// 
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+108,$LEFTMARGIN+20,$commercial_array[0]['support_documents']."\n",$wrapsize = 150,
//  $wrapbreak = "\n" , $cellwidth = 150,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
// 
// 
//  drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+154,$LEFTMARGIN+114,
//  $commercial_array[0]['city_from']."/".
//  implode('.',explode('/',$commercial_array[0]['Date'])),$wrapsize
//  =31,$wrapbreak = "\n" , $cellwidth = 70,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
// 
// drawPdfString($fontname, $fontstyle="B",$fontsize,
//  $TOPMARGIN+155,$LEFTMARGIN+17,
//  $commercial_array[0]['ship_status'],$wrapsize
//  =30,$wrapbreak = "\n" , $cellwidth = 60,
//  $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);


function dopdfheader ()
{
global $commercial_array,$buyer_details,$sender_details,$pdf,$pagecount,$fontname,$fontsize,$fontstyle,$cellheight,$strlinecount;

$pdf->AddPage();
$LEFTMARGIN=15;
$TOPMARGIN=23;
$pagecount++;

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN-5,
 $LEFTMARGIN+6,$sender_details,$wrapsize = 50,$wrapbreak = "\n" , $cellwidth =
 0, $cellheight ,$cellborder=0, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+25,
 $LEFTMARGIN+6,$buyer_details,$wrapsize = 50,$wrapbreak = "\n" , $cellwidth = 0,
 $cellheight  ,$cellborder=0, $cellalign="J",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+66,
 $LEFTMARGIN+6,$commercial_array[0]['Pre Carriage By'],$wrapsize =
 10,$wrapbreak = "\n" , $cellwidth = 25,
 $cellheight  ,$cellborder=0, $cellalign="J",$cellfill=0);
 
 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+66,
 $LEFTMARGIN+52,$commercial_array[0]['Place of Reciept'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
 
 

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+78,
 $LEFTMARGIN+6,"BY ".$commercial_array[0]['shipment_mode_name'],$wrapsize =
 10,$wrapbreak = " " , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="J",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+78,
 $LEFTMARGIN+52,$commercial_array[0]['From City'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);


 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+88,
 $LEFTMARGIN+6,$commercial_array[0]['Port of Discharge'],$wrapsize =
 50,$wrapbreak = "\n" , $cellwidth = 50,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);


 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+88,
 $LEFTMARGIN+52,$commercial_array[0]['Port of Destination'],$wrapsize =
 50,$wrapbreak = "\n" , $cellwidth = 50,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);

////////////////////////

if($pagecount==1)
{
 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+101,
 $LEFTMARGIN+6,$commercial_array[0]['Mark & Nos'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+101,
 $LEFTMARGIN+34,$commercial_array[0]['No & Kind of Pkgs'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);

$description_value=str_replace(". ",".\n ",$commercial_array[0]['Description']);

$strlinecount=substr_count($description_value,"\n");

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+101,
 $LEFTMARGIN+66,$description_value,$wrapsize =
 30,$wrapbreak = "\n" , $cellwidth = 55,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+98,
 $LEFTMARGIN+128,$commercial_array[0]['Quantity'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,$TOPMARGIN+98,
 $LEFTMARGIN+157,$commercial_array[0]['Value'],$wrapsize =
 15,$wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);

drawPdfString($fontname,$fontstyle="B",$fontsize,$TOPMARGIN+100,
 $LEFTMARGIN+113,'---------------------------------------------------------------------',$wrapsize =
 85,$wrapbreak = "\n" , $cellwidth = 85,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);

}
}


function dopdffooter()
{
 global $commercial_array,$fontname,$fontsize,$fontstyle,$cellheight;


drawPdfString($fontname,$fontstyle="B",$fontsize,$TOPMARGIN+203,
 $LEFTMARGIN+125,'---------------------------------------------------------------------',$wrapsize =
 85,$wrapbreak = "\n" , $cellwidth = 85,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);



drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+205,$LEFTMARGIN+120,
 $commercial_array[0]['total_qty'],$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);

 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+205,$LEFTMARGIN+155,
 $commercial_array[0]['total_val'],$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);

 
if($commercial_array[0]['Discount'])
{
drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+207,$LEFTMARGIN+80,"LESS DISCOUNT ".
 $commercial_array[0]['Discount']." %",$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);
$discount_value=($commercial_array[0]['total_val']*$commercial_array[0]['Discount'])/100;
drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+209,$LEFTMARGIN+155,
 number_format($discount_value,2),$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);

drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+213,$LEFTMARGIN+144,
 "--------------------------------",$wrapsize =
 50,$wrapbreak = "\n" , $cellwidth = 60,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);


drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+216,$LEFTMARGIN+155,
 number_format($commercial_array[0]['total_val']-$discount_value,2),$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);

drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+219,$LEFTMARGIN+144,
 "--------------------------------",$wrapsize =
 50,$wrapbreak = "\n" , $cellwidth = 60,
 $cellheight  ,$cellborder=0, $cellalign="R",$cellfill=0);

}

 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+262.5,$LEFTMARGIN+140,
 $commercial_array[0]['Authorised Signatory'],$wrapsize =
 45,$wrapbreak = "\n" , $cellwidth = 45,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);
 /*
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+259,$LEFTMARGIN+138,
 $commercial_array[0]['country_to'],$wrapsize=15,
 $wrapbreak = "\n" , $cellwidth = 30,
 $cellheight  ,$cellborder=0, $cellalign="C",$cellfill=0);

 
 drawPdfString($fontname, $fontstyle="B",$fontsize,
 $TOPMARGIN+270,$LEFTMARGIN+124,
 $commercial_array[0]['city_from']."/".
 implode('.',explode('/',$commercial_array[0]['Date']))."/".
 $commercial_array[0]['country_from'],$wrapsize
 =31,$wrapbreak = "\n" , $cellwidth = 70,
 $cellheight  ,$cellborder=0, $cellalign="L",$cellfill=0);
*/
}
$pdf->Output();

?>